import axios from "axios";
import router from "@/router";
import store from "@/store";
import { Message } from "element-ui";
const request = axios.create({
  baseURL: "http://localhost/heimamm/public",
  withCredentials: true,
});
request.defaults.withCredentials = true;

// 请求拦截
request.interceptors.request.use(
  (config) => {
    config.headers.token = `${store.state.login.token}`;
    return config;
  },
  (error) => {
    return Promise.reject(new Error(error));
  }
);
// 响应拦截
request.interceptors.response.use(
  (res) => {
    // console.log(res);
    // 这里是登录验证和图片验证码成功的判断
    if (res.data.code === 200 || res.statusText === "OK") {
      if (res.data.code === 206) {
        // console.log(123);
        store.commit("login/REMOVETOKEN");
        Message.error("身份过期");
        router.push("/login");
        return res.data;
      }
      if (res.data.code === 202) {
        Message.error(res.data.message);
        return Promise.reject(new Error(res.data.message));
      }
      // console.log("sdfsdf", res);
      return res.data;
    } else {
      // console.log("帝国");
      console.log(res);
      Message.error(res.data.message);
      return Promise.reject(new Error(res.data.message));
    }
  },
  (error) => {
    return Promise.reject(new Error(error));
  }
);
export default request;
